package com.xuanwu.dao.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.annotation.Resource;

import com.mchange.v2.c3p0.PooledDataSource;
import com.xuanwu.core.CoreParam;
import com.xuanwu.dao.BaseDao;
import com.xuanwu.utils.SqlUtil;

public class BaseDaoImpl implements BaseDao {

	@Resource(name = "originalDataSource")
	protected PooledDataSource originalDataSource;

	@Resource(name = "destinationDataSource")
	protected PooledDataSource destionationDataSource;
	
	protected Properties commonProp;

	public BaseDaoImpl(){
		commonProp = new Properties();
		try {
			InputStream in = BaseDaoImpl.class.getClassLoader().getResourceAsStream(CoreParam.COMMON_SQL_NAME);
			commonProp.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	public int queryObjectId(String objectName) {
		
		String originalSql = commonProp.getProperty("queryObjectId");
		String sql = SqlUtil.dealSql(originalSql, objectName);
		
		try {
			Connection connection = originalDataSource.getConnection();
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
}
